package com.cssw.fyzb.tps.api.adapay;

import com.cssw.fyzb.tps.config.adapay.AdapayConfig;
import com.cssw.fyzb.tps.service.pay.AliPayService;
import com.huifu.adapay.model.Drawcash;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/06 22:29
 */
@Function(description = "发起取现", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "orderNo", type = Type.String, description = "订单号", required = true),
        @Parameter(name = "cashType", type = Type.String, description = "取现类型：T1-T+1取现；D1-D+1取现；D0-即时取现。", required = true),
        @Parameter(name = "cashAmt", type = Type.String, description = "取现金额，必须大于0，人民币为元，保留两位小数点，如0.10、100.05等", required = true),
        @Parameter(name = "remark", type = Type.String, description = "备注", required = false)
})
@Component
public class CreateDrawcashAdapay implements Action {

    private static final Logger logger = LoggerFactory.getLogger(CreateDrawcashAdapay.class);

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private AdapayConfig adapayConfig;

    @Override
    public Response execute(Request request) throws Exception {
        String userId = request.getString("userId");
        String orderNo = request.getString("orderNo");
        String cashType = request.getString("cashType");
        String cashAmt = request.getString("cashAmt");
        String remark = request.getString("remark");

        try {
            Map<String, Object> cashParam = new  HashMap<String, Object>(5);
            cashParam.put("order_no", orderNo);
            cashParam.put("app_id", adapayConfig.getAppId());
            cashParam.put("cash_type", cashType);
            cashParam.put("cash_amt", cashAmt);
            cashParam.put("member_id", userId);
            cashParam.put("notify_url", adapayConfig.getDrawcashNotifyUrl());
            Map<String, Object> cash = Drawcash.create(cashParam);

            return Response.success().put("data", cash);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("支付发起失败");
        }
    }
}
